﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Volo.Abp;
using Volo.Abp.EntityFrameworkCore.Modeling;
using Joy.Erp.BasicArchives.Core;
using Joy.Erp.BasicArchives.SerialNumbers;

namespace Joy.Erp.BasicArchives.EntityFrameworkCore;

public static class BasicArchivesDbContextModelCreatingExtensions
{
    public static void ConfigureArchiveAggregateRoot<T>(this EntityTypeBuilder<T> b) where T : class, IArchiveAggregateRoot
    {
        b.Property(x => x.Code).HasMaxLength(ArchiveAggregateRootConsts.CodeMaxLength);
        b.Property(x => x.Code).IsRequired(ArchiveAggregateRootConsts.CodeRequired);
        b.Property(x => x.SourceType).HasMaxLength(ArchiveAggregateRootConsts.SourceTypeMaxLength);
        b.Property(x => x.SourceKey).HasMaxLength(ArchiveAggregateRootConsts.SourceKeyMaxLength);
    }

    public static void ConfigureMultiArchiveAggregateRoot<T, TChild>(this EntityTypeBuilder<T> b)
        where T : class, IMultiArchiveAggregateRoot<TChild>
        where TChild : class, IArchiveChildAggregateRoot
    {
        b.Property(x => x.Code).HasMaxLength(ArchiveAggregateRootConsts.CodeMaxLength);
        b.Property(x => x.Code).IsRequired(ArchiveAggregateRootConsts.CodeRequired);
        b.Property(x => x.SourceType).HasMaxLength(ArchiveAggregateRootConsts.SourceTypeMaxLength);
        b.Property(x => x.SourceKey).HasMaxLength(ArchiveAggregateRootConsts.SourceKeyMaxLength);
    }

    public static void ConfigureArchiveChildAggregateRoot<T>(this EntityTypeBuilder<T> b) where T : class, IArchiveChildAggregateRoot
    {
        b.Property(x => x.Id).ValueGeneratedNever();
        b.Property(x => x.SourceType).HasMaxLength(ArchiveAggregateRootConsts.SourceTypeMaxLength);
        b.Property(x => x.SourceKey).HasMaxLength(ArchiveAggregateRootConsts.SourceKeyMaxLength);
    }

    public static void ConfigureDocumentAggregateRoot<TDocument, TDocumentLine>(this EntityTypeBuilder<TDocument> b)
        where TDocument : class, IDocumentAggregateRoot<TDocumentLine>
        where TDocumentLine : class, IDocumentLineAggregateRoot
    {
        b.Property(x => x.Code).HasMaxLength(DocumentAggregateRootConsts.CodeMaxLength);
        b.Property(x => x.Code).IsRequired(DocumentAggregateRootConsts.CodeRequired);
        b.Property(x => x.BusinessDate).IsRequired(DocumentAggregateRootConsts.BusinessDateRequired);
        b.Property(x => x.DocumentStatus).IsRequired(DocumentAggregateRootConsts.DocumentStatusRequired);
        b.Property(x => x.SourceType).HasMaxLength(DocumentAggregateRootConsts.SourceTypeMaxLength);
        b.Property(x => x.SourceKey).HasMaxLength(DocumentAggregateRootConsts.SourceKeyMaxLength);
    }

    public static void ConfigureDocumentLineAggregateRoot<TDocumentLine>(this EntityTypeBuilder<TDocumentLine> b) where TDocumentLine : class, IDocumentLineAggregateRoot
    {
        b.Property(x => x.Id).ValueGeneratedNever();
        b.Property(x => x.SourceType).HasMaxLength(ArchiveAggregateRootConsts.SourceTypeMaxLength);
        b.Property(x => x.SourceKey).HasMaxLength(ArchiveAggregateRootConsts.SourceKeyMaxLength);
    }

    public static void ConfigureBasicArchives(
        this ModelBuilder builder)
    {
        Check.NotNull(builder, nameof(builder));

        //配置流水号
        builder.Entity<SerialNumber>(b =>
        {
            b.ToTable(BasicArchivesDbProperties.DbTablePrefix + "SerialNumbers", BasicArchivesDbProperties.DbSchema);
            b.ConfigureByConvention();

            b.Property(q => q.Key).IsRequired();
        });

        //从程序集中加载所有配置
        builder.AssemblyConfigure(typeof(BasicArchivesEntityFrameworkCoreModule).Assembly);
    }
}
